package com.xlisteven.voice.utils;

import java.io.IOException;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class VoiceUtils {

	public static void voice(String productName,Double productPrice) throws IOException {
		String content = productName+","+productPrice+"元";
		// 创建与微软应用程序的新连接。传入的参数是注册表中注册的程序的名称。
		ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
		try {
			// 音量 0-100
			sap.setProperty("Volume", new Variant(100));
			// 语音朗读速度 -10 到 +10
			sap.setProperty("Rate", new Variant(-2));
			// 获取执行对象
			Dispatch sapo = sap.getObject();
			// 执行朗读两次
			for (int i = 0; i < 2; i++) {
				Dispatch.call(sapo, "Speak", new Variant(content));
			}
			// 关闭执行对象
			sapo.safeRelease();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭应用程序连接
			sap.safeRelease();

		}

	}
}
